package com.janko.eurekaprovider.rabbitmq;

import com.janko.eurekaprovider.rabbitmq.config.DirectConfig;
import com.janko.eurekaprovider.rabbitmq.config.TopicConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author nyk
 * @Date 2022/4/9 14:39
 * @Version 1.0
 * @Desc 发送Direct交换机模式的消息
 */
@RestController
public class SendMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;
    /***************************************************   direct直连交换机  *************************************************************/

    @GetMapping("/sendDirect")
    public String sendDirectMessage() {
        String messageId = String.valueOf(java.util.UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        // 将消息携带绑定键值
        rabbitTemplate.convertAndSend(DirectConfig.directExchangeName, "NykTestDirectRouting", map);
        return "direct模式OK";
    }

    /***************************************************   topic交换机  *************************************************************/

    @GetMapping("/sendTopic1")
    public String sendTopicMessage1() {
        String messageId = String.valueOf(java.util.UUID.randomUUID());
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend(TopicConfig.topicExchange, TopicConfig.man, manMap);
        return "topic.man队列OK";
    }

    @GetMapping("/sendTopic2")
    public String sendTopicMessage2() {
        String messageId = String.valueOf(java.util.UUID.randomUUID());
        String messageData = "message: woman is all ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend(TopicConfig.topicExchange, TopicConfig.woman, womanMap);
        return "topic.woman队列OK";
    }

/***************************************************   扇形交换机  *************************************************************/
    @GetMapping("/sendFanout")
    public String sendFanoutMessage() {
        String messageId = String.valueOf(java.util.UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "fanout队列OK";
    }
}
